package com.leadership.api.web.controller;

import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.demo2do.core.entity.result.HttpResult;
import com.demo2do.core.utils.JsonUtils;
import com.leadership.api.handler.SurveyHandler;
import com.leadership.api.service.IdmService;
import com.leadership.api.service.SurveyService;
import com.leadership.entity.survey.SurveyResult;

/**
 * 
 * @author mohanlan
 *
 */
@RestController
@RequestMapping("")
public class SurveyApiController {
    
    private static final Logger logger = LoggerFactory.getLogger(SurveyApiController.class);
    
    @Autowired
    private SurveyHandler surveyHandler;
    
    @Autowired
    private SurveyService surveyService;
    
    @Autowired
    private IdmService idmService;
    
    @RequestMapping(value = "/survey-status", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public  @ResponseBody HttpResult surveyStatus(@RequestParam("projectId") Long projectId,
                                                  @RequestParam("item") Integer item,
                                                  @RequestParam("type") String type) {
        
        HttpResult httpResult = new HttpResult();
        
        try {
            
            logger.info("start update with project[{}] item[{}] type[{}]", projectId, item, type);
            
            List<SurveyResult> surveyResults = surveyHandler.getSurveyStatus(item, type);
            logger.info("get survey results [{}]", JsonUtils.toJson(surveyResults));
            
            surveyService.updateSurveyStatus(surveyResults, projectId, type);
            
            httpResult.success().message("同步成功");
            
        } catch (Exception e) {
            httpResult.fail("500").message("同步失败");
            logger.error("on surveyStatus error", e);
            
        }
        
        return httpResult;
    }
    
    /**
     * 
     * @param joinDate
     * @return
     */
    @RequestMapping(value = "/create-userlearning", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public  @ResponseBody HttpResult ctreateUserLearning(@RequestParam("joinDate") String joinDate) {
        HttpResult httpResult = new HttpResult();
        
        try {
            
            logger.info("start ctreateUserLearning with date[{}]", joinDate);
            
            idmService.createUserLearningMap(joinDate);
            
            httpResult.success().message("补入学习地图成功");
            
        } catch (Exception e) {
            httpResult.fail("500").message("补入学习地图失败");
            logger.error("on surveyStatus error", e);
            
        }
        
        return httpResult;
    }
}
